package com.test

import com.Main
import com.Utils
import com.boge.utils.SystemUtils
import com.logger
import com.sun.nio.file.ExtendedWatchEventModifier
import java.awt.Desktop
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.StandardWatchEventKinds
import kotlin.io.path.*
import kotlin.test.Test


class WatchTest {


    @Test
    fun test() {
        System.setProperty("runPath", SystemUtils.runPath.absolutePathString())
        System.setProperty("FILE_WATCH_PATH", "D:\\Java\\FileWatch/data.json")

        Main().start()

    }


    @Test
    fun testWatch() {


        val watchService = FileSystems.getDefault().newWatchService()


        val path = Path("D:\\WorkSpace\\LayaBox\\GameMania\\assets\\africaZoo3d\\model\\dx\\Assets")

        path.register(
            watchService, arrayOf(
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE
            ), ExtendedWatchEventModifier.FILE_TREE
        )
        println("开始监听")
        while (true) {
            val wk = watchService.take()
            val str = wk.watchable().toString()
            val events = wk.pollEvents()
            for (event in events) {
                val kind = event.kind()
                val events: Path? = event.context() as? Path
                println("kind: $kind  content: $events")
                when (kind) {
                    StandardWatchEventKinds.ENTRY_CREATE -> {

                    }

                    StandardWatchEventKinds.ENTRY_MODIFY -> {
                        if (Path(str, events!!.pathString).notExists()) {
                            println("资源删除了")
                        } else {
                            println("还在")
                        }
                    }

                    StandardWatchEventKinds.ENTRY_DELETE -> {

                    }

                    StandardWatchEventKinds.OVERFLOW -> {

                    }
                }


            }

            // 重置键，准备接收下一批事件
            if (!wk.reset()) {
                logger.warn("Key has been unregistered and will no longer be watched. file: $str")
            }

            Thread.sleep(300)

        }


    }


    @OptIn(ExperimentalPathApi::class)
    @Test
    fun read() {
        val path = Path(SystemUtils.home, "AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup")
        path.walk().forEach {
            println(it)
        }
        Desktop.getDesktop().open(path.toFile())
    }

    @Test
    fun createLnkTest() {

        Utils.createShortcutStartup(Path("D:\\Java\\FileWatch\\FileWatch.exe"))

    }


    @Test
    fun pathTest() {

        println(
            Path("F:\\tettt\\assets").javaClass
        )

    }


}